package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.TVaccinDeptMapper;
import com.hu.zhcc.shiro.dao.xsf.TVaccinNoteDeptMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.TMealsDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinDeptDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinNoteDeptDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TMeals;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinDept;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinNoteDept;
import com.hu.zhcc.shiro.service.xsf.TVaccinDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class TVaccinDeptImpl implements TVaccinDeptService {
    @Autowired
    private TVaccinDeptMapper tVaccinDeptMapper;
    @Autowired
    private TVaccinNoteDeptMapper tVaccinNoteDeptMapper;

    @Override
    public Page<TVaccinDeptDTO> queryAllTVaccinDept(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tVaccinDeptMapper.countTVaccinDept(paremeters);
        List<TVaccinDeptDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TVaccinDept> list = this.tVaccinDeptMapper.queryAllTVaccinDept(pageRequest);
            dtoList = new ArrayList<TVaccinDeptDTO>(list.size());
            for(TVaccinDept tVaccinDept : list) {
                TVaccinDeptDTO dto = new TVaccinDeptDTO();
                BeanUtils.copyProperties(tVaccinDept, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TVaccinDeptDTO>(total, dtoList);
    }

    @Override
    public TVaccinDeptDTO getById(int id) {
        TVaccinDept tVaccinDept = this.tVaccinDeptMapper.selectByPrimaryKey(id);
        if(tVaccinDept == null) {
            return null;
        }
        TVaccinDeptDTO dto = new TVaccinDeptDTO();
        BeanUtils.copyProperties(tVaccinDept, dto);
        return dto;
    }

    @Override
    public TVaccinDeptDTO saveTVaccinDept(TVaccinDeptDTO dto, TVaccinNoteDeptDTO tVaccinNoteDeptDTO) {
        TVaccinDept tVaccinDept = new TVaccinDept();
        BeanUtils.copyProperties(dto, tVaccinDept);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        tMeals.setCreateTime(simpleDateFormat.format(new Date()));
        this.tVaccinDeptMapper.insertSelective(tVaccinDept);

        TVaccinNoteDept tVaccinNoteDept = new TVaccinNoteDept();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tVaccinNoteDept.setDayDate(simpleDateFormat.format(new Date()));

        BeanUtils.copyProperties(tVaccinNoteDeptDTO, tVaccinDept);
        tVaccinNoteDeptMapper.insertSelective(tVaccinNoteDept);

        return dto;
    }

    @Override
    public int removeTVaccinDept(int id) {
        return tVaccinNoteDeptMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int updateTVaccinDept(TVaccinDept tVaccinDept,TVaccinNoteDept tVaccinNoteDept) {
        tVaccinDeptMapper.updateByPrimaryKey(tVaccinDept);
        return tVaccinNoteDeptMapper.updateByPrimaryKeySelective(tVaccinNoteDept);

    }
}
